package com.book.workbench.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.book.common.UUidUtil;
import com.book.workbench.mapper.BookMapper;
import com.book.workbench.mapper.InformationMapper;
import com.book.workbench.pojo.BorrowReturnCountP0;
import com.book.workbench.pojo.DateRequire;
import com.book.workbench.pojo.DateReturn;
import com.book.workbench.pojo.Information;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class InformationServiceImpl implements com.book.workbench.service.InformationService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private BookMapper bookMapper;
    @Override
    public Integer addInfo(Information information) {
        int addInfoResult;
        information.setReturn_date("null");
        information.setId(UUidUtil.getUUID());
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        information.setBorrow_date(dateFormat.format(date));
        information.setIslater("0");
        int searchBookIsExist = bookMapper.searchBookIsExist(information.getBook_id());
        if(searchBookIsExist==1){
            int updateBookExist = bookMapper.updateBookExist(information.getBook_id(),"0");
            addInfoResult = informationMapper.addInfo(information);
        }else{
            addInfoResult = -1;
        }
        return addInfoResult;
    }

    @Override
    public Integer updateInfo(Information information) {
        int updateInfoResult;
        Information information1 = informationMapper.searchInfoBy(information);
        if (information1==null){
            updateInfoResult = -1;
            return updateInfoResult;
        }else{
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = dateFormat.format(date);
            information.setReturn_date(nowDate);
            if(nowDate.compareTo(information1.getDue_date())<=0){
                information.setIslater("0");
            }else{
                information.setIslater("1");
            }
            updateInfoResult = informationMapper.updateInfo(information);
            bookMapper.updateBookExist(information.getBook_id(),"1");
            return updateInfoResult;
        }
    }

    /*@Override
    public PageInfo<DateReturn> searchInfoBy(DateRequire dateRequire) {
        PageHelper.startPage(dateRequire.getPageNo(),dateRequire.getPageSize());
        List<DateReturn> dateReturns = informationMapper.searchInfoBy1(dateRequire);
        PageInfo<DateReturn> page = new PageInfo<>(dateReturns,dateRequire.getPageSize());
        return page;
    }*/

    @Override
    public PageInfo<DateReturn> searchInfoBy(DateRequire dateRequire) {
        PageHelper.startPage(dateRequire.getPageNo(),dateRequire.getPageSize());
        List<DateReturn> dateReturns = informationMapper.searchInfoBy1(dateRequire);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = dateFormat.format(date);
        for (DateReturn information:dateReturns){
            System.out.println(information);
            String due_date = information.getDue_date();
            String id = information.getId();
            String return_date = information.getReturn_date();
            if(nowDate.compareTo(due_date)>0&&return_date.equals("null")){
                informationMapper.updateIslater(id,due_date);
            }
        }
        dateReturns = informationMapper.searchInfoBy1(dateRequire);
        PageInfo<DateReturn> page = new PageInfo<>(dateReturns,dateRequire.getPageSize());
        return page;
    }

    @Override
    public Integer searchCountInfoBy(DateRequire dateRequire) {
        return informationMapper.searchCountInfoBy(dateRequire);
    }

    @Override
    public Map<String, Object> getCountByDate(String date) {
        Map<String,Object> map = new HashMap<>();
        Date today = new Date();
        List<DateTime> dateTimeList;
        switch(date){
            case "week":
                dateTimeList = DateUtil.rangeToList(DateUtil.offsetDay(today,-6),today, DateField.DAY_OF_WEEK);
                break;
            case "month":
                dateTimeList = DateUtil.rangeToList(DateUtil.offsetDay(today,-29),today, DateField.DAY_OF_WEEK);
                break;
            case "month2":
                dateTimeList = DateUtil.rangeToList(DateUtil.offsetDay(today,-59),today, DateField.DAY_OF_WEEK);
                break;
            case "month3":
                dateTimeList = DateUtil.rangeToList(DateUtil.offsetDay(today,-89),today, DateField.DAY_OF_WEEK);
                break;
            default:
                dateTimeList = new ArrayList<>();
        }
        List<String> dateStrList = datetimeToDateStr(dateTimeList);
        map.put("date",dateStrList);
        List<BorrowReturnCountP0> borrowCount = informationMapper.getCountByTimeRange(date,1);
        List<BorrowReturnCountP0> returnCount = informationMapper.getCountByTimeRange(date,2);
        map.put("borrow",countList(borrowCount,dateStrList));
        map.put("return",countList(returnCount,dateStrList));
        return map;
    }

    private List<String> datetimeToDateStr(List<DateTime> dateTimeList){
        List<String> list = CollUtil.newArrayList();
        if(CollUtil.isEmpty(dateTimeList)){
            return list;
        }
        for(DateTime dateTime : dateTimeList){
            String date = DateUtil.formatDate(dateTime);
            list.add(date);
        }
        return list;
    }

    private List<Integer> countList(List<BorrowReturnCountP0> countP0List, List<String> dateRange){
        List<Integer> list = CollUtil.newArrayList();
        if(CollUtil.isEmpty(countP0List)){
            return list;
        }
        for(String date:dateRange){
            Integer count = countP0List.stream().filter(countP0 -> date.equals(countP0.getDate())).map(BorrowReturnCountP0::getCount).findFirst().orElse(0);
            list.add(count);
        }
        return list;
    }
}
